<div class="wrapper-bz-page">
    <div class="wrapper-bz-body">
        <div class="column-block block-left" style="width:500px;">
            <div class="column-title">数据库表</div>
            <div class="column-content">
                <div class="column-filter">
                    <div class="common-search-box">
                        <input style="width:300px"  id="search_input_table_in_m63" />
                    </div>
                </div>
                <div class="column-datagrid">
                    <table id="table_table_in_m63" style="width:100%;height:100%"></table>
                </div>
            </div>
        </div>
        <div class="column-block block-right" style="left:510px;">
            <div class="column-title">表字段</div>
            <div class="column-content">
                <div class="column-filter">
                    <div class="common-search-box">
                        <input style="width:300px"  id="search_input_table_field_in_m63" />
                    </div>
                </div>
                <div class="column-datagrid">
                    <table id="table_table_field_in_m63" style="width:100%;height:100%"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="dialog_add_table_in_m63"></div>
<div id="dialog_edit_table_in_m63"></div>
<div id="dialog_add_table_field_in_m63"></div>
<div id="dialog_edit_table_field_in_m63"></div>
<script>
    $(function(){
        var subject_1 = "table_in_m63";
        var subject_2 = "table_field_in_m63";
        //列表-对话框-表单
        var tableId_1 = 'table_' + subject_1;
        var tableId_2 = 'table_' + subject_2;

        var searchInputId_1 = 'search_input_' + subject_1;
        var searchInputId_2 = 'search_input_' + subject_2;

        var tableObject_1 = $('#' + tableId_1);
        var tableObject_2 = $('#' + tableId_2);

        var addDialogId_1 = 'dialog_add_' + subject_1;
        var addDialogId_2 = 'dialog_add_' + subject_2;

        var addDialogObject_1 = $("#" + addDialogId_1);
        var addDialogObject_2 = $("#" + addDialogId_2);

        var addFormId_1 = '#form_add_' + subject_1;
        var addFormId_2 = '#form_add_' + subject_2;
        var editFormId_2 = '#form_edit_' + subject_2;

        var paramDataAdd_1 = {};

        var selectLeftMainId = 0;
        var selectLeftMainItem = {};
        var configDialogAdd_1 = {
            href:'/admin/DatagridCustom/pageAdd',
            title:'新增数据表',
            queryParams:{},
            width:dialogDefaultWidth,
            height:dialogDefaultHeight,
            modal:true,
            buttons:[{
                text:'<i class="fa fa-check fa-btn-cls" style="color:green"></i>保存',
                plain:true,
                handler:function(){
                    paramDataAdd_1 = $(addFormId_1).serializeObject();
                    if(!paramDataAdd_1.datagrid_name){
                        layer.msg("请填写数据表名称");
                        return ;
                    }

                    if(!paramDataAdd_1.menu_id){
                        layer.msg("请选择菜单");
                        return ;
                    }
                    if(!paramDataAdd_1.table_id){
                        layer.msg("请选择数据表");
                        return ;
                    }

                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: '/api/DatagridCustom/addDatagridCustom',
                        async: false,
                        dataType:'json',
                        type:'post',
                        data:paramDataAdd_1,
                        success:function(resp){
                            layer.close(loading);
                            if( 1 == resp.code ){
                                $(addDialogId_1).dialog('close');
                                $(tableId_1).datagrid('reload');
                            }else{
                                layer.msg(resp.message);
                            }
                        }
                    });
                }
            },{
                text:'<i class="fa fa-times fa-btn-cls" style="color:red"></i>取消',
                plain:true,
                handler:function(){
                    $(addDialogId_1).dialog('close');
                }
            }]
        };

        var configDialogAdd_2 = {
            href:'/admin/TableField/pageAdd',
            title:'新增表字段',
            queryParams:{},
            width:dialogDefaultWidth,
            height:dialogDefaultHeight,
            modal:true,
            buttons:[{
                text:'<i class="fa fa-check fa-btn-cls" style="color:green"></i>保存',
                plain:true,
                handler:function(){
                    var formParamData = $(addFormId_2).serializeObject();
                    formParamData = $.extend(formParamData, {table_id:selectLeftMainId});
                    console.log(formParamData);
                    if(!formParamData.field_name){
                        layer.msg("请填写列标识码");
                        return ;
                    }
                    if(!formParamData.field_title){
                        layer.msg("请填写列名");
                        return ;
                    }
                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: '/api/TableField/addTableField',
                        async: false,
                        dataType:'json',
                        type:'post',
                        data:formParamData,
                        success:function(resp){
                            layer.close(loading);
                            if( 1 == resp.code ){
                                addDialogObject_2.dialog('close');
                                tableObject_2.datagrid('reload');
                            }else{
                                layer.msg(resp.message);
                            }
                        }
                    });
                }
            },{
                text:'<i class="fa fa-times fa-btn-cls" style="color:red"></i>取消',
                plain:true,
                handler:function(){
                    addDialogObject_2.dialog('close');
                }
            }]
        };
        var configDialogEdit = {
            href:'role/edit',
            title:'修改角色',
            queryParams:{},
            width:dialogDefaultWidth,
            height:dialogDefaultHeight,
            modal:true,
            buttons:[{
                text:'<i class="fa fa-check fa-btn-cls" style="color:green"></i>保存',
                plain:true,
                handler:function(){
                    paramDataEdit = $(editFormId).serializeObject();
                    console.log(paramDataEdit);
                    if(!paramDataEdit.role_name){
                        layer.msg("请填写角色名称");
                        return ;
                    }
                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: '/api/role/edit',
                        async: false,
                        dataType:'json',
                        type:'post',
                        data:paramDataEdit,
                        success:function(resp){
                            layer.close(loading);
                            if( 1 == resp.code ){
                                $(editDialogId).dialog('close');
                                $(tableId).datagrid('reload');
                            }else{
                                layer.msg(resp.message);
                            }
                        }
                    });
                }
            },{
                text:'<i class="fa fa-times fa-btn-cls" style="color:red"></i>取消',
                plain:true,
                handler:function(){
                    $(editDialogId).dialog('close');
                }
            }]
        };

        var configDatagrid_1 = {
            url:'/api/Table/getTableList',
            method: 'post',
            remoteSort:false,
            queryParams:{},
            pageList:[25,50,100],
            pageSize:25,
            pagination:true,
            clickToEdit:false,
            dblclickToEdit:false,
            singleSelect:true,
            checkOnSelect:true,
            selectOnCheck:true,
            rownumbers:false,
            striped:true,
            fitColumns:true,
            fit:true,
            frozenColumns:[[
                {field:'ck',title:'ck',checkbox:true},
            ]],
            columns:[[
                {field:'id',title:'ID',width:40,hidden:true},
                {field:'table_name',title:'表名称',width:250},
                {field:'comment',title:'备注',width:150},
            ]],
            loadFilter: function(data){
                return loadFilter(data, true);
            },
            onSelect: function(index, row){
                configDatagrid_2.queryParams = {table_id:row.id};

                if(0 == selectLeftMainId){
                    //第一次初始化
                    tableObject_2.datagrid(configDatagrid_2).datagrid("enableCellEditing");
                }else{
                    tableObject_2.datagrid(configDatagrid_2);
                }
                selectLeftMainId = row.id;
                selectLeftMainItem = row;

                SearchCombobox(searchInputId_2, tableId_2,function (val,name) {
                    configDatagrid_2.queryParams = {};
                    configDatagrid_2.queryParams[name] = val;
                    tableObject_2.datagrid(configDatagrid_2);
                },['ck','ID','类型', '排序', '状态', '创建时间', '更新时间']);
            },
            onClickRow: function(index, row){
                // console.log("enter click row");
                // selectLeftMainId = row.id;
                // configDatagrid_2.queryParams = {datagrid_id:row.id};
                // tableObject_2.datagrid(configDatagrid_2);
            },
            onCheck: function(index, row){
            },
            onUncheck: function(index, row){
            },
            onLoadSuccess: function(){
                tableObject_1.datagrid('selectRow', 0);
                $(".datagrid-header-check").html("");
            },
            toolbar:[
                {
                    text:'<span class="tool-btn-refresh"><i class="fa fa-refresh"></i>刷新</span>',
                    plain:true,
                    handler:function(){
                        tableObject_1.datagrid('reload');
                    }
                },
                {
                    text:'<span class="tool-btn-sync"><i class="fa fa-long-arrow-down"></i>同步</span>',
                    plain:true,
                    handler:function(){
                        $.ajax({
                            url:'/api/Table/synTable',
                            type:'post',
                            data: {},
                            dataType:'json',
                            async:false,
                            success:function(resp){
                                if(0 == resp.code){
                                    layer.msg(resp.message);
                                }
                                tableObject_1.datagrid('reload');
                            }
                        });
                    }
                }
            ]

        };
        var configDatagrid_2 = {
            url:'/api/TableField/getTableFieldList',
            method: 'post',
            remoteSort:false,
            queryParams:{},
            pageList:[30,50,100],
            pageSize:30,
            pagination:true,
            clickToEdit:false,
            dblclickToEdit:true,
            singleSelect:false,
            checkOnSelect:false,
            selectOnCheck:false,
            rownumbers:false,
            striped:true,
            fit:true,
            frozenColumns:[[
                {field:'ck',title:'ck',checkbox:true},
                {field:'id',title:'ID',width:40,hidden:true},
                {field:'field_name',title:'列标识码',width:150},
                {field:'field_title',title:'列名<em class="fa fa-pencil"></em>',width:150, editor:'textbox'},
                {field:'data_type',title:'类型',width:100},
                {field:'sort',title:'排序<em class="fa fa-pencil"></em>',width:60, editor:'numberbox'},
                {field:'status',title:'状态',width:80, align: 'center', formatter: function (value, row, index) {
                        if (1 == value) {
                            return '<span class="cell cell-yes"></span>';
                        } else if (2 == value) {
                            return '<span class="cell cell-no"></span>';
                        }
                    }},


            ]],
            columns:[[
                {field:'create_time',title:'创建时间',width:160},
                {field:'update_time',title:'更新时间',width:160},
                {field:'field_comment',title:'备注'},
            ]],
            loadFilter: function(data){
                return loadFilter(data, true);
            },
            onCheck: function(row){
            },
            onUncheck: function(row){
            },
            onEndEdit: function(index, row, changes) { //在完成编辑但编辑器还没有销毁之前触发
                console.log("end edit:", index, row);
                if (JSON.stringify(changes) != '{}') {
                    var updateData = $.extend({id:row.id}, changes);
                    $.ajax({
                        url:'/api/TableField/simpleUpdateTableField',
                        type:'post',
                        data: updateData,
                        dataType:'json',
                        async:false,
                        success:function(resp){
                            if(0 == resp.code){
                                layer.msg(resp.message);
                            }else{
                                tableObject_2.datagrid('updateRow', index);
                                // tableObject_2.datagrid('reload');
                            }
                        }
                    });
                }
            },
            onLoadSuccess:function(){
                // $(".switchBtn").switchbutton({
                //     height: 18,
                //     onText: "是",
                //     offText: "否",
                //     onChange: function(value){
                //         var newValue = value ? 1:0;
                //         var pageColumnId = $(this).data("id");
                //         var fieldName = $(this).data("field");
                //         toggleDatagridColumnInfo(pageColumnId, fieldName, newValue);
                //     }
                // })
            },
            toolbar:[
                {
                    text:'<span class="tool-btn-refresh"><i class="fa fa-refresh"></i>刷新</span>',
                    plain:true,
                    handler:function(){
                        tableObject_2.datagrid('reload');
                    }
                },
                {
                    text:'<span class="tool-btn-sync"><i class="fa fa-long-arrow-down"></i>同步</span>',
                    plain:true,
                    handler:function(){
                        var rows = tableObject_1.datagrid("getChecked");
                        if(0 == rows.length){
                            layer.msg("请勾选要同步的数据库表");
                            return false;
                        }
                        $.ajax({
                            url:'/api/TableField/synchronizeTableField',
                            type:'post',
                            data: {table_id: selectLeftMainId},
                            dataType:'json',
                            async:false,
                            success:function(resp){
                                if(0 == resp.code){
                                    layer.msg(resp.message);
                                }
                                tableObject_2.datagrid('reload');
                            }
                        });
                    }
                },
                {
                    text:'<span class="tool-btn-add"><i class="fa fa-plus"></i>新增</span>',
                    plain:true,
                    handler:function(){
                        addDialogObject_2.dialog(configDialogAdd_2);
                    }
                },
                {
                    text:'<span class="tool-btn-edit"><i class="fa fa-pencil"></i>编辑</span>',
                    plain:true,
                    handler:function(){
                    }
                },
                {
                    text:'<span class="tool-btn-delete"><i class="fa fa-trash"></i>删除</span>',
                    plain:true,
                    handler:function(){
                        var rows = tableObject_2.datagrid("getChecked");
                        if(0 == rows.length){
                            layer.msg("请勾选要删除的库表字段");
                            return false;
                        }

                        var arrId = [];
                        $.each(rows, function(index, item){
                            arrId.push(item.id);
                        });
                        var confirm = layer.confirm('你确定要删除?',{title:'执行操作',btn:['是','否']},function() {
                            $.ajax({
                                url: '/api/TableField/deleteMultiTableField',
                                type: 'post',
                                data: {arr_id: arrId},
                                dataType: 'json',
                                async: false,
                                success: function (resp) {
                                    layer.close(confirm);
                                    if (0 == resp.code) {
                                        layer.msg(resp.message);
                                    }
                                    tableObject_2.datagrid('reload');
                                }
                            });
                        });
                    }
                }
            ]

        };
        tableObject_1.datagrid(configDatagrid_1);

        SearchCombobox(searchInputId_1, tableId_1,function (val,name) {
            configDatagrid_1.queryParams = {};
            configDatagrid_1.queryParams[name] = val;
            tableObject_1.datagrid(configDatagrid_1);
        },['ck','ID']);


        function togglePageToolbar(pageToolbarId, fieldName, value)
        {
            console.log("update page toolbar", pageToolbarId);
            $.ajax({
                url:'/api/DatagridToolbar/toggleDatagridToolbar',
                type:'post',
                data: {id: pageToolbarId, field_name: fieldName, value:value},
                dataType:'json',
                async:false,
                success:function(resp){
                    if(0 == resp.code){
                        layer.msg(resp.message);
                    }
                }
            });
        }

    });


</script>